package com.admin.system.service;

import com.admin.base.Page;
import com.admin.system.dao.SystemGroupMapper;
import com.admin.system.entity.SystemGroup;
import com.admin.system.vo.query.SystemGroupQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 组织机构操作相关
 * @author : alex
 * @date : 2020/2/10
 */
@Service
public class SystemGroupService {

    @Autowired
    private SystemGroupMapper systemGroupMapper;

    public SystemGroup queryById(Integer id) {
        return systemGroupMapper.selectByPrimaryKey(id);
    }

    public void add(SystemGroup record) {
        Integer sort = systemGroupMapper.getMaxSort(record.getPid());
        if (sort != null) {
            record.setSort(++sort);
        } else {
            record.setSort(0);
        }
        systemGroupMapper.insertSelective(record);
    }

    /**
     * 批量删除
     */
    public void delete(Integer[] ids) {
        recurDelete(ids[0]);
    }

    public void update(SystemGroup record) {
        systemGroupMapper.updateByPrimaryKeySelective(record);
    }

    public List<SystemGroup> listByConditions(SystemGroupQuery query) {
        return systemGroupMapper.listByConditions(query);
    }

    public Long pageCount(SystemGroupQuery query) {
        return systemGroupMapper.pageCount(query);
    }

    public Page<SystemGroup> pagingByConditions(SystemGroupQuery query) {
        return systemGroupMapper.pagingByConditions(query);
    }

    public List<SystemGroup> getGroupTree() {
        List<SystemGroup> groups = systemGroupMapper.listByConditions(new SystemGroupQuery());
        return structureTree(groups);
    }

    /**
     * 保存调整后的组织机构的信息
     *
     * @param groups
     */
    public void saveGroupTree(List<SystemGroup> groups) {
        for (int i = 0; i < groups.size(); i++) {
            SystemGroup record = groups.get(i);
            if (record.getChildren() != null) {
                recurSave(record.getId(), record.getChildren());
            }
            record.setPid(0);
            record.setSort(i);
            systemGroupMapper.updateByPrimaryKeySelective(record);
        }
    }

    public List<Integer> getChildList(Integer groupId) {
        List<Integer> ids = new ArrayList<>();
        SystemGroupQuery groupQuery = new SystemGroupQuery();
        groupQuery.setPid(groupId);
        ids.add(groupId);
        recurChildIds(groupId,ids);
        return ids;
    }

    public void recurChildIds(Integer groupId, List<Integer> ids) {
        SystemGroupQuery groupQuery = new SystemGroupQuery();
        groupQuery.setPid(groupId);
        List<SystemGroup> list = systemGroupMapper.listByConditions(groupQuery);
        if (!list.isEmpty()) {
            for (SystemGroup group : list) {
                ids.add(group.getId());
                recurChildIds(group.getId(),ids);
            }
        }
    }

    /**
     * 根据ID递归删除当前及下级
     *
     * @param id
     */
    private void recurDelete(Integer id) {
        SystemGroupQuery query = new SystemGroupQuery();
        query.setPid(id);
        List<SystemGroup> childrenList = systemGroupMapper.listByConditions(query);
        if (!CollectionUtils.isEmpty(childrenList)) {
            for (SystemGroup children : childrenList) {
                recurDelete(children.getId());
            }
        }
        systemGroupMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建组织树结构
     *
     * @param groups
     * @return
     */
    private List<SystemGroup> structureTree(List<SystemGroup> groups) {
        // 第一级菜单
        List<SystemGroup> topGroup = new ArrayList<>();
        // 子菜单
        List<SystemGroup> childGroup = new ArrayList<>();
        // 进行分类
        for (SystemGroup systemGroup : groups) {
            if (systemGroup.getPid() == 0) {
                topGroup.add(systemGroup);
            } else {
                childGroup.add(systemGroup);
            }
        }
        // 给所有子菜单寻找上级菜单
        for (SystemGroup child : childGroup) {
            for (SystemGroup parent : groups) {
                if (child.getPid().equals(parent.getId())) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(child);
                    break;
                }
            }
        }
        return topGroup;
    }

    /**
     * 递归更新叶子节点信息
     *
     * @param pid
     * @param children
     */
    private void recurSave(Integer pid, List<SystemGroup> children) {
        for (int i = 0; i < children.size(); i++) {
            SystemGroup record = children.get(i);
            if (record.getChildren() != null) {
                recurSave(record.getId(), record.getChildren());
            }
            record.setSort(i);
            record.setPid(pid);
            systemGroupMapper.updateByPrimaryKeySelective(record);
        }
    }
}

